package org.test.xmlSax;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

public class SomeUtil {

	
	@Test
	public void xmlToMap() throws DocumentException{
		String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><alipay><is_success>T</is_success><request><param name=\"_input_charset\">utf-8</param><param name=\"service\">single_trade_query</param><param name=\"partner\">2088101568349711</param><param name=\"out_trade_no\">20130608174820000539</param></request><response><trade><body>商品描述01</body><buyer_email>441424b01u7.cdb@sina.cn</buyer_email><buyer_id>2088702662625488</buyer_id><discount>0.00</discount><flag_trade_locked>0</flag_trade_locked><gmt_create>2013-06-08 17:48:23</gmt_create><gmt_last_modified_time>2013-06-08 17:52:44</gmt_last_modified_time><gmt_payment>2013-06-08 17:52:44</gmt_payment><is_total_fee_adjust>F</is_total_fee_adjust><operator_role>B</operator_role><out_trade_no>20130608174820000539</out_trade_no><payment_type>1</payment_type><price>0.01</price><quantity>1</quantity><seller_email>alipay-test05@alipay.com</seller_email><seller_id>2088101568349711</seller_id><subject>测试商品01</subject><to_buyer_fee>0.00</to_buyer_fee><to_seller_fee>0.01</to_seller_fee><total_fee>0.01</total_fee><trade_no>2013060818296548</trade_no><trade_status>TRADE_SUCCESS</trade_status><use_coupon>F</use_coupon></trade></response><sign>3128995637ae440bc8a0bb1049a7caa4</sign><sign_type>MD5</sign_type></alipay>";
		
		Document document = DocumentHelper.parseText(xmlStr);
		Element rootElement = document.getRootElement();
		Element responseElement = rootElement.element("response");
        Element tradeElement  = responseElement.element("trade");
        
        List<Element> elist = tradeElement.elements();
        Map map = new HashMap();
        for (Element element : elist) {
			map.put(element.getName(), element.getText());
		}
        
        System.out.println("将相关的xml变为map");
        System.out.println(map);
	}
	
	
	
	
}
